package net.zeus.sample.rabbitmq.work;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

import net.zeus.sample.rabbitmq.utils.ConnectionUtils;

/**
 * 轮询分发
 * @author Michael
 *
 */
public class WorkMqReceiver1 {

	public static void main(String[] args) throws Exception{
		Connection conn = ConnectionUtils.getConnection();
		
		Channel channel = conn.createChannel();
		
		channel.queueDeclare(ConnectionUtils.WORK_QUEUE_NAME, false, false, false, null);
		
		DefaultConsumer consumer = new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				
				String msg = new String(body, "utf-8");
				System.out.println("get new msg-->" + msg);
				
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					System.out.println("1 done");
				}
				
			}
			
		};
		
		System.out.println("work mq receiver 1 running...");
		
		channel.basicConsume(ConnectionUtils.WORK_QUEUE_NAME, true, consumer);

	}

}
